import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

public class GameFrame extends JFrame {
    //定义窗口大小
    final int frameWidth = 800;
    final int frameHeight = 600;

    final int step = 30;
    //分数
    public static int score = 0;
    //游戏状态，零未开始，一游戏中，二暂停，三失败，四通关
    public static int state = 0;
    Image cacheImage = null;
    //定义蛇身的对象
    ArrayList<SnakeBody> snakeBody = new ArrayList<>();
    Random random = new Random();
    //食物对象
    FoodObj foodObj = getRandomFood();

    //随机食物
    public FoodObj getRandomFood() {
        int lineNum = frameHeight / step;
        int x = random.nextInt(lineNum) * step;//x={0-570]
        int y = (random.nextInt(lineNum - 1) + 1) * step;//y=[30=570]
        return new FoodObj(x, y, GameUtils.foodImg, this);
    }

    public void launch() {
        //设置窗口标题
        this.setTitle("贪吃蛇");
        //设置窗口大小
        this.setSize(frameWidth, frameHeight);
        //设置窗口相对位置
        this.setLocationRelativeTo(null);
        //设置窗口可见性
        this.setVisible(true);
        //设置窗口关闭操作  JFrame.EXIT_ON_CLOSE 关闭窗口，程序退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化两节蛇身
        snakeBody.add(new SnakeBody(0, 570, GameUtils.bodyImg, this));
        snakeBody.add(new SnakeBody(-30, 570, GameUtils.bodyImg, this));
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    switch (state) {
                        case 0://未开始
                        case 2://暂停
                            state = 1;
                            break;
                        case 1:
                            state = 2;
                            break;
                        default:
                            break;
                    }
                }
            }
        });
        while (true) {
            if (state == 1) {
                repaint();//重绘蛇画面物体运动
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public SnakeHead snakeHead = new SnakeHead(30, 570, GameUtils.rightImg, this);

    public void paint(Graphics graphics) {
        if (cacheImage ==null){
            cacheImage = this.createImage(frameWidth,frameHeight);
        }
        Graphics oldGraphics = graphics;
        graphics = cacheImage.getGraphics();
        graphics.setColor(Color.DARK_GRAY);
        graphics.fillRect(0, 0, frameWidth, frameHeight);
        //设置线条颜色
        graphics.setColor(Color.gray);
        int lineNum = frameHeight / step; //划线的数量
        for (int i = 0; i < lineNum; i++) {
            //画横线
            graphics.drawLine(0, i * step, frameHeight, i * step);
            //画竖线
            graphics.drawLine(i * step, 0, i * step, frameHeight);
        }
        //绘制蛇身
        for (int i = snakeBody.size() - 1; i >= 0; i--) {
            snakeBody.get(i).paintSelf(graphics);
        }
        //绘制蛇头
        snakeHead.paintSelf(graphics);
        //绘制食物对象
        foodObj.paintSelf(graphics);
        oldGraphics.drawImage(cacheImage,0,0,null);

    }

    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        gameFrame.launch();
    }
}
